package com.vol.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vol.entity.SysDepartment;
import com.vol.entity.UserInfo;
import com.vol.entity.pager.PageParams;
import com.vol.entity.pager.ResultPager;
import com.vol.mapper.SysDepartmentMapper;
import com.vol.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class DepartmentService {
    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;
    @Autowired
    private UserContext userContext;

    public ResultPager<SysDepartment> getPageData(PageParams pageParams){
        UserInfo userInfo = userContext.getUserInfo();
        Page<SysDepartment> page = new Page<>(pageParams.getPage(),pageParams.getRows());
        QueryWrapper<SysDepartment> qw = new QueryWrapper<SysDepartment>();
        if(pageParams.getOrder().equals("desc")) {
            qw.orderByDesc(pageParams.getSort());
        }else if(pageParams.getOrder().equals("asc")){
            qw.orderByAsc(pageParams.getSort());
        }

        sysDepartmentMapper.selectPage(page,qw);
        List<SysDepartment> records = page.getRecords();
        ResultPager<SysDepartment> resultPager = new ResultPager<>();
        resultPager.setStatus(0);
        resultPager.setRows(records);
        resultPager.setTotal((int)page.getTotal());
        return resultPager;
    }

    public void saveOrUpdate(SysDepartment sysDepartment){
        UserInfo userInfo = userContext.getUserInfo();
        if(sysDepartment.getDepartmentid()!=null){
            LambdaUpdateWrapper<SysDepartment> uw = new LambdaUpdateWrapper<>();
            uw.eq(SysDepartment::getDepartmentid,sysDepartment.getDepartmentid());
            uw.set(SysDepartment::getDepartmentname,sysDepartment.getDepartmentname());
            uw.set(SysDepartment::getDepartmentcode,sysDepartment.getDepartmentcode());
            uw.set(SysDepartment::getParentid,sysDepartment.getParentid());

            sysDepartment.setModifyid(userInfo.getUser_Id());
            sysDepartment.setModifier(userInfo.getUserName());
            sysDepartment.setModifydate(new Date());
            sysDepartmentMapper.update(sysDepartment,uw);
        }else {
            sysDepartment.setDepartmentid(UUID.randomUUID().toString());
            sysDepartment.setCreateid(userInfo.getUser_Id());
            sysDepartment.setCreator(userInfo.getUserName());
            sysDepartment.setCreatedate(new Date());
            sysDepartmentMapper.insert(sysDepartment);
        }
    }

    @Transactional
    public void  del(ArrayList<String> ids){
        for(String id:ids){
            sysDepartmentMapper.deleteById(id);
        }
    }

}
